package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import top.api.Constants.SystemConstants;
import top.api.common.ResponseResult;
import top.api.domain.dto.AddCategoryDto;
import top.api.domain.dto.CategoryUpdateDto;
import top.api.domain.entity.Article;
import top.api.domain.entity.Category;
import top.api.domain.vo.AdminCategoryListPageVo;
import top.api.domain.vo.PageVo;
import top.api.domain.vo.getCategoryInfoVo;
import top.api.enums.AppHttpCodeEnum;
import top.api.exception.SystemException;
import top.api.service.ArticleService;
import top.api.service.CategoryService;
import top.api.mapper.CategoryMapper;
import org.springframework.stereotype.Service;
import top.api.utils.BeanCopyUtils;
import top.api.domain.vo.CategoryVo;

import java.util.List;

/**
* @author 27465
* @description 针对表【sg_category(分类表)】的数据库操作Service实现
* @createDate 2023-01-29 12:39:19
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        // 查询
        List<Category> categoryList = categoryMapper.getCategoryList();
        // 拷贝到Vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        // 查询
        List<Category> categoryList = categoryMapper.getCategoryList();
        // 拷贝到Vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult getCategoryListPage(Integer pageNum, Integer pageSize, String name, String status) {
        // 动态查询
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name);
        queryWrapper.eq(StringUtils.hasText(status),Category::getStatus, status);

        // 分页
        Page<Category> pageInfo = new Page<>();
        page(pageInfo,queryWrapper);

        // 数据拷贝vo
        List<AdminCategoryListPageVo> adminCategoryListPageVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), AdminCategoryListPageVo.class);
        PageVo pageVo = new PageVo(adminCategoryListPageVos,pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(AddCategoryDto addCategoryDto) {
        if (! StringUtils.hasText(addCategoryDto.getName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }else if (! StringUtils.hasText(addCategoryDto.getStatus())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        Category category = BeanCopyUtils.copyBean(addCategoryDto, Category.class);
        categoryMapper.insert(category);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryInfo(Long id) {
        Category category = categoryMapper.selectById(id);

        getCategoryInfoVo vo = BeanCopyUtils.copyBean(category, getCategoryInfoVo.class);

        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult updateCategory(CategoryUpdateDto categoryUpdateDto) {
        Category category = BeanCopyUtils.copyBean(categoryUpdateDto, Category.class);
        categoryMapper.updateById(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteCategory(Long id) {
        // 只能删除没有被文章绑定的分类
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getCategoryId,id);

        Integer count = articleService.count(queryWrapper);

        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.CATEGORY_DELETE_ERR);
        }

        // 进行伪删除
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Category::getId,id);
        updateWrapper.set(Category::getDelFlag,SystemConstants.DelFlagNO);

        super.update(updateWrapper);

        return ResponseResult.okResult();
    }

}




